package edu.ucsd.db.bassarisk.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MultiHashMap<K, V> extends HashMap<K, List<V>> implements MultiMap<K, V> {
  private static final long serialVersionUID = -7543012475766509534L;

  @Override
  public void appendTo(K key, V value) {
    if (!this.containsKey(key)) {
      this.put(key, new ArrayList<V>());
    }
    this.get(key).add(value);
  }

  @Override
  public Iterable<V> getList(K key) {
    return this.get(key);
  }

  @Override
  public V getValue(K key, int index) {
    if (!this.containsKey(key)) {
      List<V> list = this.get(key);
      return list.get(index);
    }
    return null;
  }

  @Override
  public int getSize(K key) {
    if (!this.containsKey(key)) {
      return this.get(key).size();
    }
    return 0;
  }
}
